package cn.seapon.talker.edit;

import java.io.IOException;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.seapon.talker.LoginActivity;
import cn.seapon.talker.MainActivity;
import cn.seapon.talker.R;
import cn.seapon.talker.bean.Account;
import cn.seapon.talker.common.MessageGenre;
import cn.seapon.talker.common.TMessage;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.net.HttpRequest;

public class EditPasswdActivity extends Activity {
	private static final String TAG = "||__EditEmailActivity__||";
	private EditText old_passwd;
	private EditText new_passwd;
	private EditText new_confirm;
	private Button save_btn;
	
	private OnKeyListener keyListener = new OnKeyListener() {
		
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				return false;
			}
			return false;
		}
	};

	private static final String EDIT_MAIL_URL = "http://1.lichtung.sinaapp.com/appserver/appservice.php?request_code=2007";
	private OnClickListener backToLoginListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Account account = new Account();
			account.setName(SharedItems.userInformation.getAccount());
			account.setIs_default(false);
			SharedItems.accountDao.update(account);
			try {
				SharedItems.socket.close();
				// 停止服务
				TMessage tMessage = new TMessage();
				tMessage.setSenderId(SharedItems.userInformation.getAccount());
				tMessage.setGenre(MessageGenre.LOGOUT_REQUEST);
				Intent intent = new Intent(EditPasswdActivity.this,
						LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
				startActivity(intent);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	};
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Toast.makeText(EditPasswdActivity.this, "修改成功！",
						Toast.LENGTH_SHORT).show();
				save_btn.setOnClickListener(backToLoginListener);
				break;
			case -1:
				Toast.makeText(EditPasswdActivity.this, "输入不能为空！",
						Toast.LENGTH_SHORT).show();
				break;
			case -2:
				Toast.makeText(EditPasswdActivity.this, "密码前后不一致！",
						Toast.LENGTH_SHORT).show();
				break;
			case -3:
				Toast.makeText(EditPasswdActivity.this, "请求失败！",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.save_btn:
				String oldpwd = old_passwd.getText().toString().trim();
				String newpwd = new_passwd.getText().toString().trim();
				String pwdconfirm = new_confirm.getText().toString().trim();
				if (TextUtils.isEmpty(oldpwd) || TextUtils.isEmpty(newpwd)
						|| TextUtils.isEmpty(pwdconfirm)) {
					Message message = Message.obtain();
					message.what = -1;
					handler.sendMessage(message);
					return;
				}
				if (!newpwd.equals(pwdconfirm)) {
					Message message = Message.obtain();
					message.what = -2;
					handler.sendMessage(message);
					return;
				}
				final String url = EDIT_MAIL_URL + "&new_passwd="
						+ URLEncoder.encode(newpwd) + "&userid="
						+ SharedItems.userInformation.getAccount()
						+ "&old_passwd=" + URLEncoder.encode(oldpwd);
				Log.d(TAG, url);
				Thread thread = new Thread() {

					@Override
					public void run() {
						String retString = HttpRequest.doGetRequest(url);
						Message message = Message.obtain();
						if ("success".equals(retString)) {
							message.what = 1;
							handler.sendMessage(message);
						} else {
							message.what = -3;
							handler.sendMessage(message);
						}
					}

				};
				thread.start();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_edit_passwd);
		old_passwd = (EditText) findViewById(R.id.old_passwd);
		new_passwd = (EditText) findViewById(R.id.new_passwd);
		new_confirm = (EditText) findViewById(R.id.new_confirm);
		save_btn = (Button) findViewById(R.id.save_btn);

		save_btn.setOnClickListener(listener);
	}

}
